package com.example.wanbaqian.customView;

import android.app.Dialog;
import android.content.Context;
import android.view.Window;
import android.view.WindowManager;

import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;

import java.util.List;

import chihane.jdaddressselector.AddressProvider;
import chihane.jdaddressselector.AddressSelector;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;
import mlxy.utils.Dev;

/**
 * Created by Administrator on 2017/4/18.
 */

public class BottomAreaDialog  extends Dialog implements HttpCallback {
    private AddressSelector selector;

    public BottomAreaDialog(Context context) {
        super(context, chihane.jdaddressselector.R.style.bottom_dialog);
        this.init(context);
    }

    public BottomAreaDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.init(context);
    }

    public BottomAreaDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.init(context);
    }

    private void init(Context context) {
        this.selector = new AddressSelector(context);
        this.setContentView(this.selector.getView());
        Window window = this.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = -1;
        params.height = Dev.dp2px(context, 256.0F);
        window.setAttributes(params);
        window.setGravity(80);
    }
    public void setData(){
        selector.setAddressProvider(new AddressProvider() {
            @Override
            public void provideProvinces(AddressReceiver<Province> addressReceiver) {
                addressReceiver.send(provinces);
            }

            @Override
            public void provideCitiesWith(int provinceId, final AddressReceiver<City> addressReceiver) {
                HttpUtils.getInstance().post(GetJsonUtils.getInstance()
                                .getRequest(HttpURL.LANMEI_AREA, getContext(),
                                        new String[]{"accessid", "area_no"}
                                        , new Object[]{SPUtils.getAccessid(getContext()), provinceId})
                        , HttpURL.LANMEI_AREA+"City", CommonValue.REQUEST_CODE1, new HttpCallback() {
                            @Override
                            public void onSuccess(String content, Info info, int requestCode) {
                                List<City> cities= JsonUtils.getInstance().jsonToList(content.replaceAll("area_name","name").replaceAll("area_no","id"),City.class);
                                addressReceiver.send(cities);
                            }

                            @Override
                            public void onCacheSuccess(String content, Info info, int requestCode) {

                            }

                            @Override
                            public void onError(Info info, int requestCode) {

                            }
                        });
            }

            @Override
            public void provideCountiesWith(int cityId, final AddressReceiver<County> addressReceiver) {
                HttpUtils.getInstance().post(GetJsonUtils.getInstance()
                                .getRequest(HttpURL.LANMEI_AREA, getContext(),
                                        new String[]{"accessid", "area_no"}
                                        , new Object[]{SPUtils.getAccessid(getContext()), cityId})
                        , HttpURL.LANMEI_AREA+"County", CommonValue.REQUEST_CODE1, new HttpCallback() {
                            @Override
                            public void onSuccess(String content, Info info, int requestCode) {
                                List<County> cities= JsonUtils.getInstance().jsonToList(content.replaceAll("area_name","name").replaceAll("area_no","id"),County.class);
                                addressReceiver.send(cities);
                            }

                            @Override
                            public void onCacheSuccess(String content, Info info, int requestCode) {

                            }

                            @Override
                            public void onError(Info info, int requestCode) {

                            }
                        });
            }

            @Override
            public void provideStreetsWith(int countyId, AddressReceiver<Street> addressReceiver) {
                // blahblahblah

                addressReceiver.send(null);
            }
        });
    }
    public void setOnAddressSelectedListener(OnAddressSelectedListener listener) {
        this.selector.setOnAddressSelectedListener(listener);
    }

    public static BottomAreaDialog show(Context context) {
        return show(context, (OnAddressSelectedListener)null);
    }

    public static BottomAreaDialog show(Context context, OnAddressSelectedListener listener) {
        BottomAreaDialog dialog = new BottomAreaDialog(context, chihane.jdaddressselector.R.style.bottom_dialog);
        dialog.selector.setOnAddressSelectedListener(listener);
        dialog.show();
        return dialog;
    }

    public void getCity() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance()
                        .getRequest(HttpURL.LANMEI_AREA, getContext(),
                                new String[]{"accessid","area_no"}
                                , new Object[]{SPUtils.getAccessid(getContext()),""})
                ,HttpURL.LANMEI_AREA+"Province", CommonValue.REQUEST_CODE1, this);
    }
    List<Province> provinces;
    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        provinces= JsonUtils.getInstance().jsonToList(content.replaceAll("area_name","name").replaceAll("area_no","id"),Province.class);
        setData();
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
    }
}
